package org.feng.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;

//import io.quarkus.hibernate.orm.panache.PanacheEntity;
import io.quarkus.hibernate.orm.panache.PanacheEntityBase;
import io.quarkus.security.jpa.Password;
import io.quarkus.security.jpa.PasswordType;
import io.quarkus.security.jpa.Roles;
import io.quarkus.security.jpa.UserDefinition;
import io.quarkus.security.jpa.Username;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
//import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.transaction.Transactional;
//import javax.persistence.ManyToOne;
//import javax.persistence.SequenceGenerator;
//import javax.persistence.Table;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;

import org.h2.command.ddl.DropRole;
import org.hibernate.annotations.CreationTimestamp;
//import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.UpdateTimestamp;

import java.io.Serializable;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Entity
@UserDefinition

//@ApplicationScoped
//@Table(name = "USER")
public class User extends PanacheEntityBase {
	
    @Id
    //@SequenceGenerator( name = "personSequence", sequenceName = "person_id_seq", allocationSize = 1,initialValue = 4)
    //@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "personSequence")
    @GeneratedValue
    public Integer id;

    /**
     * 用户名 (一般是公司简称+用户实名)
     */
    @NotBlank(message = "用户名不能为空")
    //columnDefinition=" CHAR(50) "
    @Column(length=50,nullable=false)
    public String userName;//

    /**
	* 手机号,唯一,作为登陆名用
	*/
    @Column(length=20,nullable=false,updatable=false)
    @Username
    public String tel;

    /**
 	* 性别 男 女
 	*/
     @Column(length=2,nullable=false)
     public String sex="男";

    @JsonIgnore
    @NotBlank(message = "密码不能为空")
    @Column(length=21,nullable=false)
    @Password(value=PasswordType.CLEAR)
    public String password="88888888";

    @Email(message = "邮箱格式不正确")
    @Column(length=50,nullable=true)
    public String email;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm", timezone = "GMT+8")
    public Date lastLoginTime;

    /**
     * 用户类别 3 平台用户  1 服务商用户 2 供应商用户
     */
    @Column(updatable=false)
    public Byte type=1;

    /**
     * 存在状态 1 已激活  2 备份 -1 未激活 -2 删除 >0 显示  <0 不显示
     */
    //@Column(columnDefinition="1")
    public Byte status=1;
    
    /**
	* 创建时间,只读
	*/
	@CreationTimestamp //新增时更新时间
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm", timezone = "GMT+8")
	@Column(nullable=false,updatable=false)
    private LocalDateTime createTime;//=LocalDateTime.now();

    public LocalDateTime getCreateTime() {
		return createTime;
	}

	/**
	* 修改时间
	*/
	//@JsonIgnore
    @UpdateTimestamp //更新时更新时间
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm", timezone = "GMT+8")
    //@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false,updatable=false,insertable=false)
    public Timestamp modifyTime;	
    
    /**
     * 用户角色 系统管理 系统查询  售后服务 维修服务 消费用户 服务商管理 服务商运维 
     */
	@Roles
    @Column(length=100,nullable=true)
    public String roles="系统管理";
    
    //@Roles
	@Transient
    @JsonIgnore
    public List<Role> getRxoles(){
    	
    	//return EntityManager .createNamedQuery("roleId in (select ur.roleId from UserRole ur where ur.userId="+userId+")", Role.class).getResultList();
    	return  Role.//listAll();
    			find("roleId in (select ur.roleId from UserRole ur where ur.userId=?1)", id).list(); 
    	
    };
    
    //@ManyToOne
    /**
     * 服务商ID
     */
    //@JsonIgnore
    @Column(nullable=false,updatable=false)
    public Integer svrId;
    
    @Transient
    public ServiceProvider getServer(){
    	return ServiceProvider.findById(svrId);    	
    };
    
    //@ManyToOne
    /**
     * 运营商ID
     */
    @JsonIgnore
    @Column(nullable=false,updatable=false)
    public Integer ispId;
    
    @JsonIgnore
    @Transient
    public ServiceProvider getIsp(){
    	return ServiceProvider.findById(ispId);    	
    };

    /**
     * 微信标识
     */
    @Column(length=32,nullable=true)
    public String unionId; 
    @Column(length=32,nullable=true)
    public String openId;
    
    /**
     * 服务剩余时长 以秒为单位
     */
    public Integer remainingDuration;
    
    @Override
    public String toString() {
        return "User{" +
                "userId=" + id +
                ", username='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", email='" + email + '\'' +
                ", lastLoginTime=" + lastLoginTime +
                ", createTime=" + createTime +
                ", modifyTime=" + modifyTime +
                ", svrId=" + svrId +
                '}';
    }
}